﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ValueExpiry.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   Associates the value of the key value pair with the expiry of the key value pair.
//   Supports SilverlightSerializer serialisation - ISerializeObject
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Caching
{
    using System;
    using Serialization;

    /// <summary>
    /// Associates the value of the key value pair with the expiry of the key value pair.
    /// Supports SilverlightSerializer serialisation - ISerializeObject
    /// </summary>
    [Serializer(typeof(ValueExpiry))]
    public sealed class ValueExpiry : ISerializeObject
    {
        public ValueExpiry()
        {
        }

        #region Properties

        /// <summary>
        /// Gets or sets Expiry of the value.
        /// </summary>
        public DateTime Expiry { get; set; }

        /// <summary>
        /// Gets or sets Value.
        /// </summary>
        public object Value { get; set; }

        #endregion

        #region Implemented Interfaces

        #region ISerializeObject

        /// <summary>
        /// The deserialize method for supporting SilverlightSerializer.
        /// </summary>
        /// <param name="data">
        /// The data to be deserialized.
        /// </param>
        /// <returns>
        /// Returns the deserialized object.
        /// </returns>
        public object Deserialize(object[] data)
        {
            var valueExpiry = new ValueExpiry { Value = data[0], Expiry = (DateTime)data[1] };
            return valueExpiry;
        }

        /// <summary>
        /// Serialize method for supporting SilverlightSerializer.
        /// </summary>
        /// <param name="target">
        /// The object to be serialized.
        /// </param>
        /// <returns>
        /// The serialized ohject data as an array.
        /// </returns>
        public object[] Serialize(object target)
        {
            var valueExpiry = (ValueExpiry)target;
            return new[] { valueExpiry.Value, valueExpiry.Expiry };
        }

        #endregion

        #endregion
    }
}